package ru.javadevelop.vegetarian.activities;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import ru.javadevelop.vegetarian.R;
import ru.javadevelop.vegetarian.background.SyncService;
import ru.javadevelop.vegetarian.common.ExtApplication;
import ru.javadevelop.vegetarian.common.interfaces.IRefreshable;
import ru.javadevelop.vegetarian.controls.HeaderTitles;
import ru.javadevelop.vegetarian.fragments.CategoriesFragment;
import ru.javadevelop.vegetarian.fragments.HeaderFragment;
import ru.javadevelop.vegetarian.fragments.RecipesFragment;
import ru.javadevelop.vegetarian.model.Favorite;
import ru.javadevelop.vegetarian.model.Recipe;

import java.util.ArrayList;

public class HomeActivity extends Activity implements IRefreshable {
    private TextView goto_favorites;
    private HeaderFragment headerFragment;
    private CategoriesFragment categoriesFragment;
    private ComponentName service;
   // private LinearLayout fav_container;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findControls();
        fillControls();
        setListeners();
        startup();
    }

    //******************************************************************************************************************
//**********************************************************************************************************************
    private void findControls() {
        goto_favorites = (TextView)findViewById(R.id.goto_favorites);
        headerFragment = (HeaderFragment)getFragmentManager().findFragmentById(R.id.header_fragment);
        categoriesFragment = (CategoriesFragment)getFragmentManager().findFragmentById(R.id.categories_fragment);
        //fav_container = (LinearLayout)findViewById(R.id.fav_container);
    }
//**********************************************************************************************************************
    private void setListeners() {
        goto_favorites.setOnClickListener(new OnGotoFavListener());
    }
//**********************************************************************************************************************
    private void fillControls() {
        HeaderTitles headerTitles = new HeaderTitles(
                null,
                "",
                this.getString(R.string.app_name)
        );
        headerFragment.setHeaderTitles(headerTitles);
        categoriesFragment.fillControls();
        ArrayList<Favorite> favorites = Favorite.getAllForUser(ExtApplication.Session.googleAccount);

        WindowManager mWindowManager =  (WindowManager) getSystemService(WINDOW_SERVICE);
        Display mDisplay = mWindowManager.getDefaultDisplay();
        int rotation = mDisplay.getRotation();
        if ( (rotation == Surface.ROTATION_0) || (rotation == Surface.ROTATION_180))
        {
            if ( (favorites!=null) && (favorites.size()>0) )
            {
                ArrayList<Recipe> favoriteRecipes = new ArrayList<Recipe>();
                for (int i=0; i<favorites.size(); i++)
                {
                    if (i==5) break;
                    favoriteRecipes.add(Recipe.getById(favorites.get(i).recipeId));
                }
                RecipesFragment recipesFragment = new RecipesFragment(favoriteRecipes);
                recipesFragment.setArguments(getIntent().getExtras());
                getFragmentManager().beginTransaction().add(R.id.fav_container, recipesFragment).commit();
            }
        }

    }
    //******************************************************************************************************************
    private  void startup()
    {
        Intent i = new Intent(this, SyncService.class);
        service = startService(i);
    }
    //******************************************************************************************************************
    @Override
    public void refresh() {
        findControls();
        fillControls();
        setListeners();
        startup();
    }
    @Override
    public void  onResume()
    {
        super.onResume();
        refresh();
    }

    //**********************************************************************************************************************
    private class OnGotoFavListener implements View.OnClickListener{

    @Override
    public void onClick(View v) {
        Intent i = new Intent(HomeActivity.this, FavoritesActivity.class);
        HomeActivity.this.startActivity(i);
    }
}
//**********************************************************************************************************************
}
